#!/usr/bin/env python
#encoding=utf8

import pyperclip as pclip
import threading
import socket
import time

g_clip_text = ""

def get_clip_text():
    return pclip.paste()

def set_clip_text(text):
    return pclip.copy(text)

def is_clip_changed():
    global g_clip_text
    cur_text = get_clip_text()
    return g_clip_text != cur_text

def main():
    global g_clip_text
    g_clip_text = get_clip_text()

    s = socket.socket()
    s.connect(("127.0.0.1", 10085))

    while True:
        time.sleep(1)
        if is_clip_changed():
            g_clip_text = get_clip_text()
            s.send(g_clip_text.encode("utf8"))
            print("clipboard changed. text=%s" % g_clip_text)

if __name__ == '__main__':
    main()